package com.weetop.cfw.mine.activity

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.kongzue.dialog.v3.MessageDialog
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.AudioFrequencyBrowseRecordPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.AudioFrequencyBrowseRecordView
import com.weetop.cfw.bean.AudioVideoBrowsingRecordBean
import com.weetop.cfw.mine.adapter.MyCourseListAdapter
import kotlinx.android.synthetic.main.activity_audio_frequency_browse_record.*

/**
 * 管理音频浏览记录界面
 */
class AudioFrequencyBrowseRecordActivity : CommonBaseActivity(),View.OnClickListener,AudioFrequencyBrowseRecordView,OnRefreshLoadMoreListener {

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.imageAudioBrowseRecordBack -> ActivityUtils.finishActivity(this)
            R.id.textClearRecord -> tipUserClearAudioRecord()
        }
    }

    private lateinit var myCourseListAdapter:MyCourseListAdapter

    private var currentPageNumber = 1//当前的数据页数
    private var totalCount = 0//数据的总数量

    private var isRefresh = false//是否正在刷新
    private var isLoadMore = false//是否加载更多

    private val audioVideoBrowsingRecordBeanList = ArrayList<AudioVideoBrowsingRecordBean.DataBean>()

    private lateinit var audioFrequencyBrowseRecordPresenterImp: AudioFrequencyBrowseRecordPresenterImp

    override fun getLayoutId() = R.layout.activity_audio_frequency_browse_record

    override fun initView(savedInstanceState: Bundle?) {
        audioFrequencyBrowseRecordPresenterImp = AudioFrequencyBrowseRecordPresenterImp()
        audioFrequencyBrowseRecordPresenterImp.attachView(this)
        smartRefreshLayout.setOnRefreshLoadMoreListener(this)
        smartRefreshLayout.setEnableAutoLoadMore(false)
       setViewsOnClickListener(this,imageAudioBrowseRecordBack,textClearRecord)
    }

    override fun initData(savedInstanceState: Bundle?) {

        audioFrequencyBrowseRecordPresenterImp.getAudioBrowsingRecord(this,currentPageNumber)

        myCourseListAdapter = MyCourseListAdapter(R.layout.layout_item_audio_frequency,audioVideoBrowsingRecordBeanList)
        myCourseListAdapter.emptyView = emptyView
        myCourseListAdapter.setOnItemClickListener { adapter, view, position ->
            AudioDetailActivity.startAudioDetailActivity(this,audioVideoBrowsingRecordBeanList[position].infoid)
        }
        audioBrowseRecordRV.adapter = myCourseListAdapter
    }

    /**
     * 提醒用户是否清空管理音频的浏览记录
     */
    private fun tipUserClearAudioRecord(){
        MessageDialog.show(this,"提示","确定清空管理音频浏览记录？","清空","取消").setOnOkButtonClickListener { baseDialog, v ->
            audioFrequencyBrowseRecordPresenterImp.clearAudioBrowsingRecord(this@AudioFrequencyBrowseRecordActivity)
            baseDialog.doDismiss()
            true
        }
    }

    /**
     * 管理音频浏览记录清空成功
     */
    override fun clearAudioFrequencyBrowseRecordSuccess() {
        smartRefreshLayout.autoRefresh()
    }

    override fun onDestroy() {
        super.onDestroy()
        audioFrequencyBrowseRecordPresenterImp.detachView()
    }

    /**
     * 音频浏览记录获取成功
     */
    override fun getAudioBrowsingRecordSuccess(audioVideoBrowsingRecordBean: AudioVideoBrowsingRecordBean?) {
        if(audioVideoBrowsingRecordBean != null){

            totalCount = audioVideoBrowsingRecordBean.records

            if(isRefresh){
                audioVideoBrowsingRecordBeanList.clear()
                audioVideoBrowsingRecordBeanList.addAll(audioVideoBrowsingRecordBean.data)
                myCourseListAdapter.notifyDataSetChanged()
                smartRefreshLayout.finishRefresh(true)
                if(audioVideoBrowsingRecordBean.data.size > 0){
                    currentPageNumber = 1//将当前数据页数重置为1
                }
                isRefresh = false
                return
            }
            if(isLoadMore){
                audioVideoBrowsingRecordBeanList.addAll(audioVideoBrowsingRecordBean.data)
                myCourseListAdapter.notifyDataSetChanged()
                smartRefreshLayout.finishLoadMore(false)
                if(audioVideoBrowsingRecordBean.data.size > 0){
                    currentPageNumber += 1//将当前数据页数增加1
                }
                isLoadMore = false
                return
            }
            audioVideoBrowsingRecordBeanList.addAll(audioVideoBrowsingRecordBean.data)
            myCourseListAdapter.notifyDataSetChanged()
        }
    }

    /**
     * 音频浏览记录获取失败
     */
    override fun getAudioBrowsingRecordFailed() {
        smartRefreshLayout.finishRefresh(false)
        smartRefreshLayout.finishLoadMore(false)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if(totalCount <= audioVideoBrowsingRecordBeanList.size){
            showNativeShortToast("暂无更多数据")
            smartRefreshLayout.finishLoadMoreWithNoMoreData()
        }else{
            isLoadMore = true
            audioFrequencyBrowseRecordPresenterImp.getAudioBrowsingRecord(this,currentPageNumber + 1)
        }
    }
    override fun onRefresh(refreshLayout: RefreshLayout) {
        isRefresh = true
        audioFrequencyBrowseRecordPresenterImp.getAudioBrowsingRecord(this,1)
    }
}
